package com.example.weibo3.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.weibo3.MainActivity;
import com.example.weibo3.PhotoActivity;
import com.example.weibo3.R;
import com.example.weibo3.bean.WeiboItem;

import java.io.Serializable;
import java.util.List;

public class GridViewAdapter extends ArrayAdapter<String> {
    private List<String> images;
    public GridViewAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
        super(context, resource, objects);
        images = objects;
    }

    @NonNull
    @Override
    // 返回每一条微博
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 加载布局文件，生成一个view
        View view = LayoutInflater.from(getContext()).inflate(R.layout.gridview_item, parent, false);

        // 获取数据，通过position获取数组中对应的一项数据，实际上就是图片的网络地址
        String item = getItem(position);

        // 什么数据渲染到什么组件上
        // 图片渲染填充
        ImageView imageView = view.findViewById(R.id.image);  // 图片组件
        Glide.with(getContext())
                .load(item)
                .into(imageView);

        // 给图片组件添加点击事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到新的Activity
                Intent intent = new Intent(getContext(), PhotoActivity.class);
                intent.putExtra("image", (Serializable) images);
                getContext().startActivity(intent);
            }
        });
        return view;
    }
}
